package com.itestor.iptv.modules.alert.actions;

import java.io.File;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.itestor.iptv.global.GlobalConstant;
import com.itestor.iptv.global.utils.ITestFileUtil;
import com.itestor.iptv.modules.alert.service.RobotLogService;
import com.itestor.iptv.modules.alert.vo.DwloadFileInfo;
import com.itestor.iptv.modules.alert.vo.RobotLogBean;

/**
 * 
 * @author Mr.li
 * 
 */
public class RobotSyncFileDownloadAction extends Action {

    /**
     * 
     */
    private static final Logger logger = Logger
            .getLogger(RobotSyncFileDownloadAction.class);

    /**
     * 业务对象
     */
    private RobotLogService robotLogService;


    /**
     * 
     */
    private Map<String, Long> downLoadMap = new ConcurrentHashMap<String, Long>();
    

    /**
     * 
     */
    private String downloadDirPath;

    /**
     * 
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String status = "";
        
        String idRobotLog = request.getParameter("id");
        String type = request.getParameter("type");

        if (StringUtils.isEmpty(idRobotLog)) {
            return null;
        }
        
        RobotLogBean robotLogBean = robotLogService.getRobotLogBean(Long
                .parseLong(idRobotLog));
      
        String localFile = null;
        String remoteFile = null;
        DwloadFileInfo info = new DwloadFileInfo();
        
        if ("pcapFile".equals(type)) {
            localFile = downloadDirPath+File.separator+robotLogBean.getLocalPcapFileURL();            
            remoteFile = robotLogBean.getRemotePcapFileURL();
            info.setFileName(robotLogBean.getRobotlog().getPcapFile());
            info.setLocalURL(robotLogBean.getLocalPcapFileURL());
        } else if ("prePcapFile".equals(type)) {
            localFile = downloadDirPath+File.separator+robotLogBean.getLocalPrePcapFileURL();            
            remoteFile = robotLogBean.getRemotePrePcapFileURL();
            info.setFileName(robotLogBean.getRobotlog().getPrePcapFile());
            info.setLocalURL(robotLogBean.getLocalPrePcapFileURL());
        } else if ("videoFile".equals(type)) {
            localFile = downloadDirPath+File.separator+robotLogBean.getLocalVideoFileURL();            
            remoteFile = robotLogBean.getRemoteVideoFileURL();
            info.setFileName(robotLogBean.getRobotlog().getVideoFile());
            info.setLocalURL(robotLogBean.getLocalVideoFileURL());
        }

        logger.info("localURL :" + info.getLocalURL());  
        
        remoteFile=remoteFile.replace('\\','/');       
        
        logger.info("remoteURL:" + remoteFile);
        logger.info("localFile:" + localFile);

        //设置本地下载路径
        info.setLocalFile(localFile);        
        if (null != localFile) {
            boolean isExist = ITestFileUtil.isExistsFile(localFile);
            if (!isExist) {                
                request.setAttribute("localFile", localFile);
                try{
                    ITestFileUtil.copyFile(remoteFile, localFile);
                }catch (Exception e) {
                   
                }
            } else {
                request.setAttribute("isExist", isExist);                
                request.setAttribute("localFile", localFile);                
                logger.info("local file is exists ! ");
            }
        }
        
        request.setAttribute("downloadBean", info);        
        return mapping.findForward(GlobalConstant.FORWARD_SUCCESS);        
    }

    /**
     * @param pDownLoadMap
     */
    public void setDownLoadMap(Map<String, Long> pDownLoadMap) {
        downLoadMap = pDownLoadMap;
    }
    
    /**
     * 
     * @param pRobotLogService
     */
    public void setRobotLogService(RobotLogService pRobotLogService) {
        robotLogService = pRobotLogService;
    }
    
    /**
     * 
     * @param pDownloadDirPath
     */
    public void setDownloadDirPath(String pDownloadDirPath) {
        downloadDirPath = pDownloadDirPath;
    }

    
    
}
